<?php
require_once "phing/Task.php";

class DeleteLineTask extends Task {

    /**
     * The pattern of delete line
     */
    private $pattern = null;

    /**
     * The target file for line delete
     * @var string
     */
    protected $filename = "";

    /**
     * The setter for the attribute "pattern"
     */
    public function setPattern($str) {
        $this->pattern = $str;
    }
    public function setFilename($filename){
        $this->filename = $filename;
    }
    /**
     * The init method: Do init steps.
     */
    public function init() {
      // nothing to do here
    }
    /**
     * The main entry point method.
     */
    public function main() {
        $lines = file($this->filename);
        $lines = array_map('trim', $lines);
        foreach($lines as $i => $line){
            // reserve original empty line
            if($line != "\n"){
                $p = addcslashes($this->pattern, '$/');
                $p = "/{$p}/iu";
//                echo $p . "\n";
                if(preg_match($p, $line) == 1){
                    unset($lines[$i]);
                }
            }
        }
        copy($this->filename, $this->filename . ".bak");
        file_put_contents($this->filename, join("\n", $lines));
    }
}
?>